<?php

namespace App\Admin\Controllers;

use App\Models\Device;
use App\Models\DeviceLog;
use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
// use Encore\Admin\Facades\Admin;
use Dcat\Admin\Admin;
use App\Models\ProjectUser;
use Illuminate\Support\Facades\DB;


class DeviceLogController extends Controller
{
    public function index(Content $content)
    {
        // $projectId = session('now_project');
        // if (empty($projectId)) {
        //     admin_warning('请选择指定 项目 后再查看相关数据', '');
        //     return redirect('/admin');
        // }
        Admin::script(
            <<<JS
    $(".ar-tag").remove();
JS            
        );
        return $content
            ->header('设备日志')
            ->description()
//            ->body(Callout::make('即将在下个版本发布，敬请期待~'))
            ->body($this->grid());
    }

    protected function grid()
    {
        $projectIds = ProjectUser::where('user_id', Admin::user()->id)->pluck('projectId')->toArray();
        $device = Device::whereIn('projectId', $projectIds)->pluck('deviceId');
        // $device = Device::where(['projectId'=>session('now_project')])->pluck('deviceId');
        return new Grid(DeviceLog::whereIn('deviceId',$device), function (Grid $grid) {
            $grid->showColumnSelector();
            $grid->disableActions();
            $grid->disableCreateButton();
            // $grid->filter(function($filter){
            //     // 去掉默认的id过滤器
            //     $filter->disableIdFilter();
            //     $filter->equal('device_type','设备类型')->select(function(){
            //         $type = Db::table('xfy_devicetype')->get(['dtypeId','typename'])->toArray();
            //         $res = array_combine(array_column($type, 'dtypeId'), $type);
            //         foreach ($res as $key => &$value) {
            //             $res[$key] = $value['typename'];
            //         }
            //         return $res;
            //     });
            //     $filter->between('created_at', '开始日期')->datetime();
            //     $filter->equal('deviceId', '设备ID');
            //     // $filter->equal('address', '上报IP地址');
            //     // $filter->equal('pname', '阿里云设备名');
            //     //$filter->in('gatewayId', '上报字段名')->multipleSelect(['key' => 'value']);
            // });
            $grid->column('id', __('ID'));
            $grid->column('deviceId', __('设备编号'));
            $grid->column('devicename', __('设备名称'))->display(function () {
                $devicename = DB::table('xfy_device')->where(['deviceId'=>$this->deviceId])->value('devicename');
                // $projectname = DB::table('xfy_project')->where(['projectId'=>$projectId])->value('projectname');
                return $devicename;
            });
            $grid->column('device_type', __('设备类型'))->display(function () {
                $typename = DB::table('xfy_devicetype')->where(['dtypeId'=>$this->device_type])->value('typename');
                return $typename;
            });
            $grid->column('projectname', __('项目名称'))->display(function () {
                $projectId = DB::table('xfy_device')->where(['deviceId'=>$this->deviceId])->value('projectId');
                $projectname = DB::table('xfy_project')->where(['projectId'=>$projectId])->value('projectname');
                return $projectname;
            });
            $grid->column('msg', __('报警详情'));
            $grid->column('result', __('处理结果'));
            $grid->column('user_id', __('处理人员'))->display(function () {
                $name = DB::table('admin_users')->where(['id'=>$this->user_id])->value('name');
                return $name;
            });
            $grid->column('created_at', __('上报时间'));
            $grid->column('updated_at', __('处理时间'));
            $grid->column('status',trans('状态'))->using(['0'=>'未处理','1'=>'已处理']);
            // 过滤器
            $grid->filter(function (Grid\Filter $filter) {
                // $group = function (Grid\Filter\Group $group) {
                //     $group->equal('等于');
                //     $group->gt('大于');
                //     $group->lt('小于');
                //     $group->nlt('大于等于');
                //     $group->ngt('小于等于');
                //     $group->like('包含');
                //     $group->startWith('包含（起始）');
                //     $group->endWith('包含（结束）');
                //     $group->match('正则');
                // };
                $filter->equal('deviceId')->width('350px');
                // $filter->group('date', $group)->date()->width('350px');
                $filter->between('created_at', 'Between')->datetime();
            });
            $grid->export()->xlsx();
        });
    }
}